package com.ece.demo.normal;

import java.io.InputStream;
import java.util.Map;

import org.lwjgl.opengl.GL20;

import dev.ece.core.light.Material;
import dev.ece.core.light.MaterialLocationHandler;
import dev.ece.core.shader.Shader;
import dev.ece.core.texture.Texture;

public class NormalMapShader extends Shader {


	public NormalMapShader(InputStream vertexShaderInputStream,
			InputStream pixelShaderInputStream, Map<String, String> functions) {
		super(vertexShaderInputStream, pixelShaderInputStream, functions);
	}

	@Override
	protected String getVertexHeader() {
		StringBuffer s = new StringBuffer();
		s.append(Material.getMaterialStructString());
		s.append(super.getVertexHeader());
		return s.toString();
	}

	@Override
	protected String getPixelHeader() {
		return this.getVertexHeader();
	}

	
	@Override
	protected String getVersion() {
		return "#version 330 core";
	}


	private MaterialLocationHandler materialLocationHanlder;
	
	private int textureHandler;
	
	private int normalHandler;
	
	@Override
	protected void onBindAttribLocations(int program) {
		GL20.glBindAttribLocation(program, 0, "ece_Vertex");
		GL20.glBindAttribLocation(program, 1, "ece_TexCoord");
	}

	@Override
	protected void onInitializeHandler(int program) {
		materialLocationHanlder = MaterialLocationHandler.newInstance(program, "material.");
		textureHandler = GL20.glGetUniformLocation(program, "texture");
		normalHandler = GL20.glGetUniformLocation(program, "normalMapping");
	}

	public void glNormal(Texture normal) {
		GL20.glUniform1i(normalHandler, normal.bind(1));
	}
	
	public void glTexture(Texture texture) {
		GL20.glUniform1i(textureHandler, texture.bind(0));
	}
	
	public void glMaterial(Material material) {
		material.glUniformMaterial(materialLocationHanlder);
	}
	
	@Override
	public void clearup() {
		GL20.glDeleteShader(textureHandler);
		GL20.glDeleteShader(normalHandler);
		materialLocationHanlder.clearup();
		super.clearup();
	}

}
